package com.kewu.system.controller;

import com.github.pagehelper.PageInfo;
import com.kewu.common.global.ResponseData;
import com.kewu.common.global.request.PageParam;
import com.kewu.system.api.entity.SysQuartz;
import com.kewu.system.service.SysQuartzService;
import lombok.RequiredArgsConstructor;
import org.quartz.SchedulerException;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;

/**
 * 定时任务(SysQuartz)表控制层
 *
 * @author wangning
 * @since 2024-05-29 14:07:36
 */
@RestController
@RequestMapping("/sysQuartz")
@RequiredArgsConstructor
public class SysQuartzController {
    /**
     * 服务对象
     */
    private final SysQuartzService sysQuartzService;

    /**
     * 新增定时任务
     *
     * @param sysQuartz 实体
     * @return 新增结果
     */
    @PostMapping("/add")
    public ResponseData<SysQuartz> add(@RequestBody SysQuartz sysQuartz) throws ParseException, SchedulerException {
        return ResponseData.success(sysQuartzService.add(sysQuartz));
    }

    /**
     * 暂停任务
     *
     * @param sysQuartz
     * @return
     * @throws SchedulerException
     */
    @PostMapping("/pauseJob")
    public ResponseData<Void> pauseJob(@RequestBody SysQuartz sysQuartz) throws SchedulerException {
        sysQuartzService.pauseJob(sysQuartz);
        return ResponseData.success();
    }

    /**
     * 恢复任务
     *
     * @param sysQuartz
     * @return
     * @throws SchedulerException
     */
    @PostMapping("/resumeJob")
    public ResponseData<Void> resumeJob(@RequestBody SysQuartz sysQuartz) throws SchedulerException {
        sysQuartzService.resumeJob(sysQuartz);
        return ResponseData.success();
    }

    /**
     * 删除任务
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("/delete/{id}")
    public ResponseData<Boolean> delete(@PathVariable("id") Integer id) throws SchedulerException {
        return ResponseData.success(sysQuartzService.delete(id));
    }

    /**
     * 更新任务cron表达式
     *
     * @param sysQuartz
     * @return
     */
    @PostMapping("/updateCron")
    public ResponseData<Void> updateCron(@RequestBody SysQuartz sysQuartz) throws SchedulerException, ParseException {
        sysQuartzService.updateCron(sysQuartz);
        return ResponseData.success();
    }

    /**
     * 分页查询
     *
     * @param sysQuartz 筛选条件
     * @return 查询结果
     */
    @GetMapping("/page")
    public ResponseData<PageInfo<SysQuartz>> page(SysQuartz sysQuartz, PageParam pageParam) {
        return ResponseData.success(sysQuartzService.page(sysQuartz, pageParam));
    }

    /**
     * 编辑数据
     *
     * @param sysQuartz 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
    public ResponseData<SysQuartz> update(@RequestBody SysQuartz sysQuartz) {
        return ResponseData.success(sysQuartzService.update(sysQuartz));
    }

}

